package model;

import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;

import com.mysql.jdbc.Connection;
//import com.mysql.jdbc.PreparedStatement;
import com.mysql.jdbc.Statement;

public class Question {
	

	private int id;
	private String intitule;
	private String Description;
	private Vector<Reponse> listeReponse;

	public Question(int id, String intitule, String description, Vector<Reponse> listeReponse) {
		this.id = id;
		this.intitule = intitule;
		this.Description = description;
		this.listeReponse = listeReponse;
	}
	
	public Question(){
		
	}


	public int getId(){
		return id;
	}
	
	public void setId(int id_){
		this.id = id_;
	}
	
	public String getIntitule(){
		return intitule;
	}
	
	public void setIntitule(String intitule_){
		this.intitule = intitule_;
	}
	
	public String getDescription(){
		return Description;
	}
	
	public void setDescription(String description){
		this.Description = description;
	}
	
	
	public Vector<Reponse> getListeReponses(){	
		return this.listeReponse;
	}
	
	public void setListeReponse(Vector<Reponse> listReponse) {
		this.listeReponse = listReponse;
	}
	
	
	public void addQuestionBD(String _intitule, String _description) throws ClassNotFoundException{
		
		String url = "jdbc:mysql://localhost:3306/Base";
		String utilisateur = "root";
		String motdepasse = "mysqlroot";
		Connection connexion = null;
		
		try{Class.forName("com.mysql.jdbc.Driver");
    	connexion = (Connection) DriverManager.getConnection(url, utilisateur, motdepasse);
    	
    	//Gestion des caractères d'échappement
    	PreparedStatement statement = connexion.prepareStatement("INSERT into questions (intitule, description) VALUES (?, ?)");
    	 
    	statement.setString(1, _intitule );
    	statement.setString(2, _description );
    	 
    	statement.executeUpdate();
    	
//    	connexion.createStatement().execute("INSERT into questions (intitule, description) VALUES ('"+_intitule+"', '"+_description+"')");
			
		
		} catch ( SQLException e ) {
        	System.out.println("Erreur lors de l'ajout de la question");
			} finally {
	            if ( connexion != null )
	                try {
	                    connexion.close();
	                	} catch ( SQLException ignore ) {
	          }
			}
		
	}
	
public void killQuestion(String _id) throws ClassNotFoundException{
		
		String url = "jdbc:mysql://localhost:3306/Base";
		String utilisateur = "root";
		String motdepasse = "mysqlroot";
		Connection connexion = null;
		
		try{Class.forName("com.mysql.jdbc.Driver");
    	connexion = (Connection) DriverManager.getConnection(url, utilisateur, motdepasse);
    	Statement query = (Statement) connexion.createStatement();
    	Statement query2 = (Statement) connexion.createStatement();
    	
    	//----------------------Suppression de la question------------------------
    	ResultSet rs = query.executeQuery("SELECT * FROM questions WHERE idQ='"+_id+"'");
    	
    	if (rs.next())
    	{
    		System.out.println("Suppression de la question en cours...");
    		String sql = "DELETE FROM questions WHERE idQ="+_id+";";

    		System.out.println(""+sql);
    		query.execute(sql);
    		System.out.println("->Question supprimée !");
    	
    	
	    	//----------------------Suppression des réponses associées------------------------
	    	ResultSet rsRep = query.executeQuery("SELECT * FROM reponses WHERE idQ='"+_id+"'");
	    	
	    	while(rsRep.next())
	    	{
	    		System.out.println("Suppression des réponses associées...");
	    		
	    		query2.execute("DELETE FROM reponses WHERE idQ="+_id+";");
	    		System.out.println("->Réponses supprimées");
	    	}
    	}
    	
    	
    	
    	
		} catch ( SQLException e ) {
        	System.out.println("ERREUR : Impossible de supprimer la Question et/ou les réponses associées");
			} finally {
	            if ( connexion != null )
	                try {
	                    connexion.close();
	                	} catch ( SQLException ignore ) {
	          }
			}
		
	}
	
}
